<?php

class Update extends Controller {
	
	/**
	 * Constructor
	 */
	function Update(){
		parent::Controller();
		$this->load->model('Update_model');
		$this->load->helper('xml');
	}
	
	
	/**
	 * url redirected
	 */
	function _remap($fromversion)
	{
	    $this->_update($fromversion);
	}
	
	/**
	 * xml update generate
	 */
	function _update($fromversion){
		
		$data['version'] = $this->Update_model->get_db_version();
		$data['packages'] = $this->_table($this->Update_model->get_new_packages($fromversion));
		
		$this->load->view('update/update',$data);
	}
	
	
	function _table($table){
	 
	 	$lengthTable = count($table);
	 	if ($lengthTable == 0){
	 		
	 		return null;
	 	}
	 	$releases = array();
	 	$branches = array();
	 	$packages = array();
	 	$releaseIndex = 0;
	 	$branchIndex = 0;
	 	$packageIndex = null;
	 	
		for($i = 0; $i < $lengthTable; $i++){
			
			$releases[$releaseIndex] = $this->_releaseTable($table[$i]);
			$releaseIndex++;
			
			if ($i < $lengthTable - 1){
				
				if ($table[$i]['branch_id'] != $table[$i+1]['branch_id']){
					
					$branches[$branchIndex] = $this->_branchTable($table[$i],$releases);
					$branchIndex++;
					$releases = array();
					$releaseIndex = 0;
				}
				
				if ($table[$i]['pkg_id'] != $table[$i+1]['pkg_id']){
					
					$packages[$packageIndex] = $this->_packageTable($table[$i],$branches);
					$packageIndex++;
					$releases = array();
		 			$branches = array();
					$releaseIndex = 0;
		 			$branchIndex = 0;
				}
			}
		}
		
		$branches[$branchIndex] = $this->_branchTable($table[$lengthTable - 1],$releases);
		$packages[$packageIndex] = $this->_packageTable($table[$lengthTable - 1],$branches);
		return $packages;
	}
	
	
	function _releaseTable($release){
		
		if ($release['rls_id'] != ''){
			$r = array (
				'rls_id' => $release['rls_id'],
				'version' => xml_convert($release['version']),
				'branch' => $release['branch'],
				'rls_last_up' => $release['rls_last_up'],
				'release_note' => xml_convert($release['release_note']),
				'rlshash' => $release['rlshash'],
				'rls_status' => $release['rls_status'],
				'type' => $release['type']
			);
		}else {
			
			$r = array ();
		}
		
		return $r;
	}
	
	
	function _branchTable($branch,$releases){
		
		if ($branch['branch_id'] != ''){
			$b = array (
				'branch_id' => $branch['branch_id'],
				'bch_name' => xml_convert($branch['bch_name']),
				'package' => $branch['package'],
				'bch_creation' => $branch['bch_creation'],
				'bch_last_up' => $branch['bch_last_up'],
				'description' => xml_convert($branch['description']),
				'bch_status' => $branch['bch_status'],
				'releases'	=> $releases
			);
		}else {
			
			$b = array ();
		}
		
		return $b;
	}
	
	
	function _packageTable($package,$branches){
		
		$p = array (
			'pkg_id' => $package['pkg_id'],
			'pkg_name' => xml_convert($package['pkg_name']),
			'website' => xml_convert($package['website']),
			'resp_usr' => $package['resp_usr'],
			'licence' => xml_convert($package['licence']),
			'category' => xml_convert($package['category']),
			'short_desc' => xml_convert($package['short_desc']),
			'pkg_creation' => $package['pkg_creation'],
			'pkg_last_up' => $package['pkg_last_up'],
			'pkg_status' => $package['pkg_status'],
			'branches'	=> $branches
		);
		
		return $p;
	}
	

}

?>
